package com.yxq.mdt.app.modules.pm.ui;


import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.launcher.ARouter;
import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.ToastUtils;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.application.App;
import com.yxq.mdt.app.base.BaseTabActivity;
import com.yxq.mdt.app.base.BaseTabBean;
import com.yxq.mdt.app.dialog.CostumerDialog;
import com.yxq.mdt.app.modules.bottomsheet.GoodsAttrBottomSheetDialogFragment;
import com.yxq.mdt.app.modules.goodsdetail.bean.GoodsDetailBean;
import com.yxq.mdt.app.modules.main.ui.MainActivity;
import com.yxq.mdt.app.modules.market.bean.MallGoodsBean;
import com.yxq.mdt.app.modules.order.ui.OrderConfirmActivity;
import com.yxq.mdt.app.modules.personal.ui.activity.MainPersonalCustomerServiceAc;
import com.yxq.mdt.app.modules.pm.bean.GoodsCommentBean;
import com.yxq.mdt.app.modules.pm.model.GoodsModel;
import com.yxq.mdt.app.modules.pm.presenter.GoodsPresenter;
import com.yxq.mdt.app.modules.pm.presenter.IGoodsContract;
import com.yxq.mdt.app.modules.webview.ui.MiWebViewActivity;
import com.yxq.mdt.app.net.API;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author L
 * @time
 * @describe 礼包详情页面
 */
//@Route(path = RouteTable.ROUTER_ACTIVITY_GIFT_PACKAGE_DETAIL)
public class GoodsDetailTabActivity extends BaseTabActivity<BaseTabBean, Fragment, GoodsPresenter> implements IGoodsContract.View {
    @Autowired
    String id = "";
    List<Fragment> fragmentList;
    GoodsDetailIndexFragment goodsDetailIndexFragment;
    GoodsDetailDetailFragment goodsDetailDetailFragment;
    GoodsDetailCommentFragment goodsDetailRecommendFragment;
    List<BaseTabBean> tabBeanList;
    @BindView(R.id.goods_detail_layout_kf)
    LinearLayout goodsDetailLayoutKf;
    @BindView(R.id.goods_detail_layout_collect)
    LinearLayout goodsDetailLayoutCollect;
    @BindView(R.id.goods_detail_layout_cart)
    LinearLayout goodsDetailLayoutCart;
    @BindView(R.id.goods_detail_tv_add_cart)
    TextView goodsDetailTvAddCart;
    @BindView(R.id.goods_detail_tv_buy)
    TextView goodsDetailTvBuy;
    @BindView(R.id.goods_detail_tab_bottom_layout)
    LinearLayout goodsDetailTabBottomLayout;
    @BindView(R.id.goods_detail_iv_collect)
    ImageView goodsDetailIvCollect;
    GoodsAttrBottomSheetDialogFragment goodsAttrBottomSheetDialogFragment;


    @Override
    public int getLayoutId() {
        return R.layout.activity_gift_package_detail_tab;
    }

//    @Override
//    public void initView() {
//        goodsDetailIndexFragment = new GoodsDetailIndexFragment();
//        goodsDetailIndexFragment.setArguments(getIntent().getExtras());
//        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
//        if (!goodsDetailIndexFragment.isAdded()) {
//            transaction.add(R.id.arch_vp_view_pager, goodsDetailIndexFragment);
//        }
//        transaction.commitAllowingStateLoss();
//        giftPackageDialog();
//    }

    @Override
    public List<BaseTabBean> getTabList() {
        fragmentList = new ArrayList<>();
        goodsDetailIndexFragment = new GoodsDetailIndexFragment();
        goodsDetailIndexFragment.setArguments(getIntent().getExtras());
        goodsDetailDetailFragment = new GoodsDetailDetailFragment();
        goodsDetailDetailFragment.setArguments(getIntent().getExtras());
        goodsDetailRecommendFragment = new GoodsDetailCommentFragment();
        goodsDetailRecommendFragment.setArguments(getIntent().getExtras());
        fragmentList.add(goodsDetailIndexFragment);
        fragmentList.add(goodsDetailDetailFragment);
        fragmentList.add(goodsDetailRecommendFragment);
        tabBeanList = new ArrayList<>();
        tabBeanList.add(new BaseTabBean(0, "商品", 0));
        tabBeanList.add(new BaseTabBean(1, "详情", 1));
        tabBeanList.add(new BaseTabBean(2, "评价", 2));
        return tabBeanList;
    }

    @Override
    public Fragment getFragment(BaseTabBean tab) {
        Fragment fragment = fragmentList.get(tab.tabId);
        return fragment;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        ARouter.getInstance().inject(this);
        App.getInstance().addActivity(this);
        mViewPager.setOffscreenPageLimit(3);
        mPresenter = new GoodsPresenter(this, new GoodsModel());
        if (!TextUtils.isEmpty(id)) {
            mPresenter.getGoods(id);
        } else {
            mPresenter.getGoods(getIntent().getStringExtra("id"));
        }
        giftPackageDialog();
    }

    @OnClick({R.id.goods_detail_layout_kf, R.id.goods_detail_layout_collect, R.id.goods_detail_layout_cart})
    public void onClicked(View view) {
        switch (view.getId()) {
            case R.id.goods_detail_layout_kf://客服
                Intent intent = new Intent(mActivity, MainPersonalCustomerServiceAc.class);
                startActivity(intent);
                break;
            case R.id.goods_detail_layout_collect://首页
                intent = new Intent(mActivity, MainActivity.class);
                intent.putExtra("tab", 0);
                startActivity(intent);
                finish();
                break;
            case R.id.goods_detail_layout_cart://购物车
                //判断是否有多个规格，多个规格需要弹出框选择规格
                if (goodsDetailIndexFragment.getGoodsDetailBean() != null) {
                    goodsAttrBottomSheetDialogFragment = new GoodsAttrBottomSheetDialogFragment(goodsDetailIndexFragment.getGoodsDetailBean());
                    goodsAttrBottomSheetDialogFragment.setBottomSheetDialogInterface((attrBean, attr, num) -> {
                        //构造规格
                        GoodsDetailBean data = JSON.parseObject(JSON.toJSONString(goodsDetailIndexFragment.getGoodsDetailBean()), GoodsDetailBean.class);
                        List<GoodsDetailBean.AttrBean> attrBeanList = data.getAttr();
                        attrBeanList.set(0, attrBean);
                        data.setAttr(attrBeanList);
                        data.setNum(num);
                        Intent intent1 = new Intent(mActivity, OrderConfirmActivity.class);
                        intent1.putExtra("gift", true);
                        intent1.putExtra("from", "goods");
                        intent1.putExtra("data", JSON.toJSONString(data));
                        startActivity(intent1);
                    });
                    goodsAttrBottomSheetDialogFragment.show(getSupportFragmentManager(), "GoodsAttrBottomSheetDialogFragment");
                }
                break;
        }
    }

    /**
     * 团长协议弹出框
     */
    private void giftPackageDialog() {
        CostumerDialog dialog = new CostumerDialog.Builder(this, R.style.custom_dialog_no_match)
                .setContentView(R.layout.dialog_gift_package_alert_layout)
                .setCancelable(true)
                .create();


        TextView content = dialog.findViewById(R.id.tv_content_hint);
        content.setOnClickListener(v -> {
            //查看团长协议
            Intent it = new Intent(mActivity, MiWebViewActivity.class);
            it.putExtra("url", API.TEAM_WITHDRAWAL);
            startActivity(it);
        });

        //继续
        TextView sure = dialog.findViewById(R.id.tv_cancel);
        sure.setText("知道了");
        sure.setOnClickListener(v -> {
            dialog.dismiss();
        });
        dialog.show();
    }

    @Override
    public void onLoadSuccess(GoodsDetailBean goodsDetailBean) {
    }

    @Override
    public void onLoadFailed(String err) {
        ToastUtils.showShort(err);
    }

    @Override
    public void onLoadRecommendSuccess(MallGoodsBean mallGoodsBean) {

    }

    @Override
    public void onLoadRecommendFailed(String err) {

    }

    @Override
    public void onAddSuccess(String err) {
    }

    @Override
    public void onAddFailed(String err) {
        ToastUtils.showShort(err);
    }

    @Override
    public void onRemoveSuccess(String err) {
    }

    @Override
    public void onRemoveFailed(String err) {
        ToastUtils.showShort(err);
    }

    @Override
    public void onLoadCommentSuccess(GoodsCommentBean goodsCommentBean) {

    }

    @Override
    public void onLoadCommentFailed(String err) {

    }

    @Override
    public void onAddCartSuccess(String result) {
        ToastUtils.showShort(result);
    }

    @Override
    public void onAddCartFailed(String err) {
        ToastUtils.showShort(err);
    }

}
